package com.service.lotterySystem.service.impl;

import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.RegexUtil;
import com.service.config.utils.Result;
import com.service.lotterySystem.dao.ReceiveAddressDao;
import com.service.lotterySystem.dto.AddressDto;
import com.service.lotterySystem.dto.ReceiveAddressDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.entity.ReceiveAddress;
import com.service.lotterySystem.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.service.config.constant.Constant.DATA_FORMAT_WRONG;
import static com.service.config.constant.Constant.PARAMETER_NOT_FOUND;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 17:33
 * @description：
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Resource
    private ReceiveAddressDao addressDao;


    /**
     * 查询用户所有的收货地址
     * @param uid
     * @return
     */
    @Override
    public List<ReceiveAddressDto> queryAddressByUid(String uid) {
        List<ReceiveAddress> receiveAddressList = addressDao.queryAddressByUid(uid);
        if (null ==receiveAddressList || receiveAddressList.size()<=0)
        {
            return null;
        }
        List<ReceiveAddressDto> receiveAddressDtos = ModelMapperUtil.strictMapList(receiveAddressList, ReceiveAddressDto.class);
        receiveAddressDtos = receiveAddressDtos.stream().sorted(Comparator.comparing(ReceiveAddressDto::getrType).reversed()).collect(Collectors.toList());
        return receiveAddressDtos;
    }

    /**
     * 填写地址信息
     * @param addressDto
     * @return
     */
    @Override
    public Result addAddress(AddressDto addressDto) {
        //校验手机号
        if (addressDto.getrTel()==null || "".equals(addressDto.getrTel()))
        {
            return Result.failure(PARAMETER_NOT_FOUND,"当前手机号输入为空");
        }
        boolean mobileSimple = RegexUtil.isMobileSimple(addressDto.getrTel());

        if(!mobileSimple){
            return Result.failure(DATA_FORMAT_WRONG,"手机号格式有误");
        }
        if (addressDto.getrType()) {
            ReceiveAddress receiveAddress = new ReceiveAddress();
            receiveAddress.setUId(addressDto.getuId());
            List<ReceiveAddress> addressList = addressDao.queryAll(receiveAddress);
            addressList.forEach(receiveAddress1 -> receiveAddress1.setRType(false));
            receiveAddress = ModelMapperUtil.strictMap(addressDto, ReceiveAddress.class);
            receiveAddress.setId(IdUtil.getStringId());
            receiveAddress.setRAddTime(new Date());
            receiveAddress.setRUpdateTime(new Date());
            receiveAddress.setRType(true);
            addressList.add(receiveAddress);
            addressDao.insertOrUpdateBatch(addressList);
        }else {
            ReceiveAddress receiveAddress = ModelMapperUtil.strictMap(addressDto, ReceiveAddress.class);
            receiveAddress.setId(IdUtil.getStringId());
            receiveAddress.setRType(true);
            receiveAddress.setRAddTime(new Date());
            receiveAddress.setRUpdateTime(new Date());
            ReceiveAddress receiveAddress1 = new ReceiveAddress();
            receiveAddress1.setUId(addressDto.getuId());
            List<ReceiveAddress> addressList = addressDao.queryAll(receiveAddress1);
            if (addressList.size() > 0) {
                List<ReceiveAddress> c = addressList.stream().filter(ReceiveAddress::getRType).collect(Collectors.toList());
                if (c.size() > 0) {
                    receiveAddress.setRType(false);
                }
            }
            addressDao.insert(receiveAddress);
        }
        return Result.ok();
    }

    /**
     * 膝盖收货地址
     * @param addressDto
     * @return
     */
    @Override
    public Result updateAddress(AddressDto addressDto) {
        if (addressDto.getrType()) {
            ReceiveAddress receiveAddress = new ReceiveAddress();
            receiveAddress.setUId(addressDto.getuId());
            List<ReceiveAddress> addressList1 = new ArrayList<>();
            List<ReceiveAddress> addressList = addressDao.queryAll(receiveAddress);
            addressList.forEach(receiveAddress1 -> {
                if (receiveAddress1.getId().equals(addressDto.getId())){
                    addressList1.add(receiveAddress1);
                }else {
                    receiveAddress1.setRType(false);
                }
            });
            addressList.removeAll(addressList1);
            addressDao.insertOrUpdateBatch(addressList);
            ReceiveAddress receiveAddress1 = ModelMapperUtil.strictMap(addressDto, ReceiveAddress.class);
            addressDao.update(receiveAddress1) ;
        }else {
            ReceiveAddress receiveAddress1 = ModelMapperUtil.strictMap(addressDto, ReceiveAddress.class);
            addressDao.update(receiveAddress1) ;
        }
        return Result.ok();
    }

    /**
     * 删除收货地址
     * @param addressId
     * @return
     */
    @Override
    public Result deleteById(String addressId) {
        addressDao.deleteById(addressId);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result defaultAddress(String userId,String addressId) {
        List<ReceiveAddress> receiveAddresses = addressDao.queryAddressByUid(userId);
        receiveAddresses.stream().forEach(receiveAddress -> {
            if (receiveAddress.getId().equals(addressId)) {
                receiveAddress.setRType(true);
            }else {
                receiveAddress.setRType(false);
            }
        });
        addressDao.insertOrUpdateBatch(receiveAddresses);
        return Result.ok();
    }

    /**
     * 根据活动id查询下属所有   中奖   用户的所有地址
     * @param aid
     * @return
     */
    @Override
    public List<ReceiveAddressDto> queryPrizedUserAddressByAid(String aid) {
        List<ReceiveAddress> addressInfos = addressDao.queryPrizedAddressByAid(aid);
        if (addressInfos !=null && addressInfos.size()>0)
        {
            return ModelMapperUtil.strictMapList(addressInfos,ReceiveAddressDto.class);
        }
        return null;
    }
}
